﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.InfoConfig.Controller;
using MINERP.DAL;

namespace MINERP.InfoConfig
{
    public partial class FrmActionManager :FrmMDIChildFormTemplate
    {
        private ActionController _controller = new ActionController();
        tb_Action _action = null;
        private static FrmActionManager instance = null; 

        private FrmActionManager()
        {
            InitializeComponent();
        }

        public static FrmActionManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmActionManager();
                }
                return instance;
            }
        }

        private void FrmActionManager_Load(object sender, EventArgs e)
        {

            _controller.Grid = gridControlAction;
            _controller.View = gridViewAction;
            _controller.GetData();

            _controller.ComoBox = cmbRole;
            _controller.LinkRole();

        }

        private void gridViewAction_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _action = (tb_Action)gridViewAction.GetFocusedRow();
            if (_action != null)
            {
                tb_Action action = _controller.GetActionObj(_action.ActionId);
                txtActionName.Text = _action.ActionName;
                txtControlName.Text = _action.ControlName;
                txtFormName.Text = _action.FormName;
                cmbRole.Text = _action.tb_Power.Name;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtActionName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_Action action = new tb_Action();
                action.ActionName = txtActionName.Text;
                action.ControlName = txtControlName.Text;
                action.FormName = txtFormName.Text;

                _controller.Add(action);

                //link datagrid
                _controller.Grid = gridControlAction;
                _controller.View = gridViewAction;
                _controller.GetData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtActionName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_Action action = _controller.GetActionObj(_action.ActionId);
                action.ActionName = txtActionName.Text;
                action.ControlName = txtControlName.Text;
                action.FormName = txtFormName.Text;

                _controller.Update(action);


                //refresh datagrid
                _controller.Grid = gridControlAction;
                _controller.View = gridViewAction;
                _controller.GetData();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        //
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                tb_Action action = _controller.GetActionObj(_action.ActionId);
                _controller.Delete(action);

                //refresh datagrid
                _controller.Grid = gridControlAction;
                _controller.View = gridViewAction;
                _controller.GetData();
            }
        }
        //
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtActionName.Enabled = false;
            cmbRole.Enabled = false;
            txtControlName.Enabled = false;
            txtFormName.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }

        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtActionName.Text == string.Empty)
            {
                txtActionName.Focus();
                MessageBox.Show("操作名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbRole.Text == string.Empty)
            {
                cmbRole.Focus();
                MessageBox.Show("角色不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtControlName.Text == string.Empty)
            {
                txtControlName.Focus();
                MessageBox.Show("控件名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtFormName.Text == string.Empty)
            {
                txtFormName.Focus();
                MessageBox.Show("窗体名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtActionName.Enabled = bl;
            cmbRole.Enabled = bl;
            txtControlName.Enabled = bl;
            txtFormName.Enabled = bl;

            txtActionName.Text = string.Empty;
            cmbRole.Text = string.Empty;
            txtControlName.Text = string.Empty;
            txtFormName.Text = string.Empty;


            if (bl)
            {
                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtActionName.Enabled = bl;
            cmbRole.Enabled = bl;
            txtControlName.Enabled = bl;
            txtFormName.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void FrmActionManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }


    }
}
